import { defineStore } from 'pinia';// 创建用户相关的仓库
import type { UserState } from './types/type';// 引入state数据类型文件
import { SET_TOKEN, GET_TOKEN } from '@/utils/token';
import { constantRoute } from '@/router/routes';// 引入路由
import { getUserInfo } from '@/api/user';
import { ElMessage } from 'element-plus';

// 创建用户小仓库
let useUserStore = defineStore('User', {
    // 小仓库储存数据的地方
    state: (): UserState => {
        return {
            token: GET_TOKEN(),//用户唯一标识
            isHead: false,//是否为技术负责人
            isExamine: false,//是否为审核人
            userPurview: null,
            authorityId: "",
            userInfo: {
                menuRoutes: constantRoute as any, //仓库存储生成菜单需要的数组
                uuid: '',
                userName:'', //用户名称
                nickName: '',
                headerImg: '', //用户头像
                sideMode: '',
                baseColor: '',
                activeColor: '',
                authorityId: 0,
                ID: 1,
            }
        }
    },
    // 事件函数
    actions: {
        // 初始化用户信息
        initUseInfo () {
            this.token = GET_TOKEN();
            this.userInfo.menuRoutes = constantRoute as any;
            this.userInfo.uuid = '';
            this.userInfo.userName = '';
            this.userInfo.nickName =  '';
            this.userInfo.headerImg = '';
            this.userInfo.sideMode = '';
            this.userInfo.baseColor = '';
            this.userInfo.activeColor = '';
            this.userInfo.authorityId = 0;
            this.userInfo.ID = 1;
        },

        // 保存token
       saveToken (data: string) {
           this.token = data;
           SET_TOKEN(this.token);
        },

        // 退出登录：清空token,初始化用户信息
        delToken () {
            SET_TOKEN("")
            this.token = GET_TOKEN();
            localStorage.clear();
            this.initUseInfo();
            ElMessage.success("退出成功！");
        },

        // 获取用户信息的方法
       async getUseInfo() {
            const resData = await getUserInfo().then((res) => {
                return res.data;
            })
            if (resData.code === 7) {
                ElMessage.error("获取用户信息失败！");
                return;
            } else {
                this.userInfo.nickName = resData.data.nickName;
                this.userInfo.uuid = resData.data.uuid;
                this.userInfo.headerImg = resData.data.headerImg;
                this.userInfo.userName = resData.data.userName;
                this.userInfo.sideMode = resData.data.sideMode;
                this.userInfo.baseColor = resData.data.baseColor;
                this.userInfo.activeColor= resData.data.activeColor;
                this.userInfo.authorityId = resData.data.authorityId;
                this.userInfo.ID = resData.data.ID;

                
                // 判断是否为审核人
                if(this.userInfo.authorityId === 333) {
                    this.isExamine = true;
                } else {
                    this.isExamine = false;
                };

                // 判断是否为技术负责人
                if(this.userInfo.authorityId === 777) {
                    localStorage.setItem("isHead", "true");
                    this.isHead = true;
                } else {
                    localStorage.setItem("isHead", "false");
                    this.isHead = false;
                };
            }
        },

       
    },
    // 数据类型转换或计算属性
    getters: {

    }
})

// 对外暴漏获取小仓库的方法
export default useUserStore